<?php

class Member_model extends CI_Model {

    function __construct() {
        parent::__construct();
    }

    function checkMemberLogin($email, $pass) {
        $this->db->where('lb_email', $email);
        $this->db->where('lb_password', md5($pass));
        $this->db->where('bl_active', 1);
        $query = $this->db->get('member');
        if ($query->row()) {
            return $query->row_array();
        } else {
            return false;
        }
    }

    function check_email($email) {
        $this->db->where('lb_email', $email);
        //$this->db->where('member_id!=',$member_id);
        $query = $this->db->get('member');
        if ($query->row()) {
            return $query->row_array();
        } else {
            return false;
        }
    }
    
    function save($data) {

        $id = (int) $this->uri->segment(3);
        if ($id != 0) {
            $this->db->where('member_id', $id);
            if ($this->db->update('member', $data)) {
                return true;
            } else {
                return false;
            }
        } else {

            if ($this->db->insert('member', $data)) {
                return $this->db->insert_id();
            } else {
                return false;
            }
        }
    }

    function add_line($data) {
        if (!empty($data)) {
            if ($this->db->insert('webchat_lines', $data)) {
                return true;
            } else {
                return false;
            }
        }
    }

    function get_member($member_id) {
        $this->db->where('member_id', $member_id);
        $query = $this->db->get('member');
        if ($query->row()) {
            return $query->row_array();
        } else {
            return false;
        }
    }
    
    function checkEmailAvaliable($email, $member_id) {
        if(!empty($member_id) && !empty($member_id)) {
            $this->db->where('member_id <>', $member_id);
            $this->db->where('lb_email', $email);
            $query = $this->db->get('member');
            if (!$query->row()) {
                return true;
            } else {
                return false;
            }
        }
        return true;
    }

    function save_member($member_id, $data) {
        if ($member_id != 0) {
            $this->db->where('member_id', $member_id);
            if ($this->db->update('member', $data)) {
                return true;
            } else {
                return false;
            }
        } else {
            if ($this->db->insert('member', $data)) {
                return $this->db->insert_id();
            } else {
                return false;
            }
        }
    }

    function select_rememberme($hashkey) {
        $this->db->where('hashkey', $hashkey);
        $query = $this->db->get('rememberme');
        return $query->row();
    }

    function insert_rememberme($data) {
        if ($this->db->insert('rememberme', $data)) {
            return $this->db->insert_id();
        } else {
            return false;
        }
    }

    function delete_rememberme($hashkey) {
        $this->db->where('hashkey', $hashkey);
        if ($this->db->delete('rememberme')) {
            return true;
        } else {
            return false;
        }
    }

}

?>
